package dd.prog2.exercises.set12.a2;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class VierGewinnt {

	public static void main(String[] args) {
		new VierGewinnt().start();
	}

	public void start() {
		// buttons & texfield
		final JButton reset = new JButton("Start new game");
		// final Coin[] buttons = new Coin[42];
		final JTextField status = new JTextField(
				"Status: waiting for the player 1 turn");
		final JTextArea info = new JTextArea(
				("\nRED: Player 1\nYELLOW: Player 2"));
		// board object
		final Board myBoard = new Board();
		// BEGIN local ActionListener
		class ButtonListener implements ActionListener {
			Coin b;
			int i;

			public ButtonListener(Coin b, int i) {
				this.b = b;
				this.i = i;
			}

			@Override
			public void actionPerformed(ActionEvent ae) {

				if (ae.getSource() == reset) {
					// reset board

				} else {
					// playing
					if (!myBoard.getGameOver()) {
						myBoard.setTurn(i);// save turn to board
						status.setText("Status: waiting for the player "
									+ myBoard.getPlayer() + " turn");
					}
					if (myBoard.getGameOver()) { // game over?
						if (myBoard.getWinner() != 0) // check winner
							status.setText("Status: Game Over. Winner is Player "
									+ myBoard.getWinner());
						else
							status.setText("Status: Game Over. No Winner");
					}

				}
			}
		}
		// END Local ActionListener
		JFrame frame = new JFrame("Vier gewinnt v2"); // create Frame
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setLayout(null);
		frame.setLocation(250, 250);
		frame.setResizable(false);
		frame.setSize(305, 370);

		Panel buttonPanel = new Panel(); // buttons
		buttonPanel.setLayout(new GridLayout(6, 7));
		buttonPanel.setSize(305, 250);
		buttonPanel.setLocation(0, 0);
		buttonPanel.setBackground(Color.GREEN);
		// //////////////////////////////////
		int a = 0;
		for (int j = 0; j < 8; j++) {
			for (int j2 = 0; j2 < 7; j2++) {

				if (a < 42) {
					myBoard.buttons[a] = new Coin(j, j2);
					myBoard.buttons[a].setDisabled();
					if(a>34)
						myBoard.buttons[a].setEnabled(); // re-enable bottom buttons
					myBoard.buttons[a].addActionListener(new ButtonListener(
							myBoard.buttons[a], a));
					buttonPanel.add(myBoard.buttons[a]);
				} else {
					break;
				}
//				System.out.println(a+" "+myBoard.buttons[a].isEnabled);
				a++;
			}
		}

		status.setLocation(0, 245); // textfield
		status.setSize(305, 20);
		status.setEditable(false);
		status.setHorizontalAlignment(JTextField.LEFT);

		reset.setLocation(0, 265); // reset button
		reset.setSize(300, 25);
		reset.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				myBoard.reset();
				status.setText("Status: Starting new Game. waiting for Player 1");
			}
		});

		info.setLocation(0, 290); // textfield
		info.setSize(305, 60);
		info.setEditable(false);
		// info.setHorizontalAlignment(JTextField.LEFT);

		frame.add(status);
		frame.add(buttonPanel);
		frame.add(reset);
		frame.add(info);

		frame.setVisible(true);

	}

}
